package com.navi.controller;

import com.alibaba.fastjson.JSONObject;
import com.navi.entity.ErrorMessageInfoEntity;
import com.navi.service.IRabbitService;
import com.navi.util.LogUtil;
import com.rabbitmq.http.client.domain.QueueInfo;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.Map;

import static com.navi.constant.GenericDef.HMS_ERROR_Q;
import static com.navi.constant.GenericDef.REDIS_ERROR_MESSAGE_KEY_FORMAT;

/**
 * @author jay
 */
@Log
@RestController
@RequestMapping("/errorQueue")
public class QueryErrorQueueInfoController {

    @Autowired
    private JedisPool jedisPool;
    @Autowired
    private IRabbitService rabbitService;

    @RequestMapping(value = "/", method = RequestMethod.GET)
    public Map<String, Map<String, ErrorMessageInfoEntity>> getAll() {
        Map<String, Map<String, ErrorMessageInfoEntity>> errorMessageMap = new HashMap<>();

        try (Jedis jedis = jedisPool.getResource()) {
            rabbitService.getQueueInfo().stream()
                    .filter(q -> q.getName().startsWith(HMS_ERROR_Q))
                    .map(QueueInfo::getName)
                    .forEach(queueName -> {
                        final String tableKey = String.format(REDIS_ERROR_MESSAGE_KEY_FORMAT, queueName);
                        Map<String, String> strMap = jedis.hgetAll(tableKey);
                        Map<String, ErrorMessageInfoEntity> errorMessageInfoMap = new HashMap<>();
                        strMap.keySet().forEach(v ->
                                errorMessageInfoMap.put(v, JSONObject.parseObject(strMap.get(v), ErrorMessageInfoEntity.class))
                        );
                        if (errorMessageInfoMap.size() > 0) {
                            errorMessageMap.put(queueName, errorMessageInfoMap);
                        }
                    });
            return errorMessageMap;
        } catch (Exception e) {
            LogUtil.pringStackTrace(e);
        }
        return null;
    }

    @RequestMapping(value = "/{queueName}", method = RequestMethod.GET)
    public Map<String, ErrorMessageInfoEntity> getByQueue(@PathVariable String queueName) {
        Map<String, ErrorMessageInfoEntity> errorMessageMap = new HashMap<>();

        try (Jedis jedis = jedisPool.getResource()) {
            final String tableKey = String.format(REDIS_ERROR_MESSAGE_KEY_FORMAT, queueName);
            Map<String, String> strMap = jedis.hgetAll(tableKey);
            strMap.keySet().forEach(v ->
                    errorMessageMap.put(v, JSONObject.parseObject(strMap.get(v), ErrorMessageInfoEntity.class))
            );
            return errorMessageMap;
        } catch (Exception e) {
            LogUtil.pringStackTrace(e);
        }
        return null;
    }

    //todo delete
}
